﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Studio77.AbbLinkLink.Util
{
    public class LLKLogicHelper
    {
        private static int[,] mapData;

        /// <summary>
        /// 连接类型
        /// 0-none
        /// 1-line
        /// 2-one conor
        /// 3-two conor
        /// </summary>
        public static int Type;

        public static int lineX1;
        public static int lineY1;
        public static int lineX2;
        public static int lineY2;

        /// <summary>
        /// 判断两点能否连线
        /// </summary>
        /// <param name="gameData"></param>
        /// <param name="x1"></param>
        /// <param name="y1"></param>
        /// <param name="x2"></param>
        /// <param name="y2"></param>
        internal static bool CanEliminate(int[,] gameData, int x1, int y1, int x2, int y2, int row, int column)
        {
            mapData = gameData;
            return IsLined(x1, y1, x2, y2) ||
                    IsOneConner(x1, y1, x2, y2, row, column) ||
                    IsTwoConner(x1, y1, x2, y2, row, column);
        }

        private static bool IsLined(int x1, int y1, int x2, int y2)
        {
            int max, min, flag;
            if (x1 == x2)
            {
                if (y1 > y2)
                {
                    max = y1;
                    min = y2;

                }
                else
                {
                    max = y2;
                    min = y1;
                }
                flag = 1;
                for (int i = min + 1; i < max; i++)
                    if (mapData[x1, i] != -1)
                    {
                        flag = 0;
                        break;
                    }
                if (flag == 1)
                {
                    Type = 1;
                    return true;
                }
            }
            else if (y1 == y2)
            {
                if (x1 > x2)
                {
                    max = x1;
                    min = x2;

                }
                else
                {
                    max = x2;
                    min = x1;
                }
                flag = 1;
                for (int i = min + 1; i < max; i++)
                    if (mapData[i, y1] != -1)
                    {
                        flag = 0;
                        break;
                    }
                if (flag == 1)
                {
                    Type = 1;
                    return true;
                }
            }

            return false;
        }

        private static bool IsOneConner(int x1, int y1, int x2, int y2, int row, int column)
        {
            for (int i = 0; i < row + 2; i++)
                for (int j = 0; j < column + 2; j++)
                {
                    if (mapData[i, j] != -1)
                        continue;
                    if (IsLined(x1, y1, i, j) && IsLined(i, j, x2, y2))
                    {
                        Type = 2;
                        lineX1 = i;
                        lineY1 = j;
                        return true;
                    }
                }

            return false;
        }

        private static bool IsTwoConner(int x1, int y1, int x2, int y2, int row, int column)
        {
            for (int i1 = 0; i1 < row + 2; i1++)
            {
                for (int j1 = 0; j1 < column + 2; j1++)
                {
                    for (int i2 = 0; i2 < row + 2; i2++)
                    {
                        for (int j2 = 0; j2 < column + 2; j2++)
                        {
                            if (mapData[i1, j1] != -1)
                                continue;
                            if (mapData[i2, j2] != -1)
                                continue;

                            if (IsLined(x1, y1, i1, j1) && IsLined(i1, j1, i2, j2) && IsLined(i2, j2, x2, y2))
                            {
                                Type = 3;
                                lineX1 = i1;
                                lineY1 = j1;
                                lineX2 = i2;
                                lineY2 = j2;
                                return true;
                            }
                        }

                    }

                }
            }

            return false;

        }
    }
}
